﻿using UnityEditor;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Editor.Tools.UI
{
    public static class UICheck
    {
        [MenuItem("GameObject/UICheck/ClearRaycastTarget")]
        public static void ClearRaycastTarget()
        {
            var selected = Selection.activeGameObject;
            if (selected == null)
            {
                return;
            }

            var graphics = selected.GetComponentsInChildren<Graphic>();
            foreach (var graphic in graphics)
            {
                if (graphic.GetComponent<Selectable>() != null  || graphic.GetComponent<IEventSystemHandler>() != null)
                {
                    continue;
                }
                graphic.raycastTarget = false;
            }
            
            // 保存修改到预制体
            var prefab = PrefabUtility.GetCorrespondingObjectFromSource(selected);
            if (prefab != null)
            {
                PrefabUtility.SaveAsPrefabAsset(selected, PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(prefab), out bool success);
            }
            
            
        }
    }
}